package com.springboot.file.fastdfs;

import lombok.extern.slf4j.Slf4j;
import org.csource.fastdfs.ProtoCommon;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @description:
 * @author: zyj
 * @create: 2020-11-25 13:50
 **/
@Service
@Slf4j
public class FastDFSUtil {

    @Value("${fdfs.http.http_secret_key:123456}")
    private String securityKey;

    @Value("${fdfs.http.myFileUrl:123456}")
    private String myFileUrl;



    /***
    * @Description: fastdfs 添加token
    * @Param: [path]
    * @return: java.lang.String
    * @Author: zyj
    * @Date: 2020/11/25
    */
    public String getTokenFastDfs(String path){
        if(StringUtils.isEmpty(path)){
            return path;
        }
        log.info("需要添加校验的地址,path:{} ===>securityKey:{}", path, securityKey);


        try {

            // 获取当前时间戳
            int ts = (int)(System.currentTimeMillis()/1000);

            // 获取Token, 传入的文件ID不要含有分组信息
            //组名
            String group = path.split("/")[0];
            //文件名
            String filePath = path.substring(group.length()+1, path.length());
            String token =  ProtoCommon.getToken(filePath, ts, securityKey);
            return path+"?token=" + token + "&ts=" + ts;
        }catch (Exception e){
            log.error("fastdfs 路径添加token失败", e);
        }

        return path;
    }



    /***
     * @Description: fastdfs 添加token  含url
     * @Param: [path]
     * @return: java.lang.String
     * @Author: zyj
     * @Date: 2020/11/25
     */
    public String getTokenFastDfsUrl(String path){
        path = myFileUrl +"/"+getTokenFastDfs(path);
        return path;
    }


}
